package core.SparkStreaming;

import org.apache.spark.SparkConf;
import org.apache.spark.streaming.Duration;
import org.apache.spark.streaming.api.java.JavaReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;

public class Spark02_SOCKET {

    public static void main(String[] args) {

        SparkConf sparkConf = new SparkConf();
        sparkConf.setMaster("local[*]");
        sparkConf.setAppName("Spark02_SOCKET");
        try {

            JavaStreamingContext jsc = new JavaStreamingContext(sparkConf, new Duration(3 * 1000));


            /**
             * 通过环境对象对接SOCKET数据源，获取数据模型，进行数据处理
             */
            JavaReceiverInputDStream<String> stringJavaReceiverInputDStream = jsc.socketTextStream("127.0.0.1", 9999);


            stringJavaReceiverInputDStream.print();

            /**
             * 启动数据采集器
             */
            jsc.start();

            /**
             * 等待数据采集器结束，如果采集器结束运行，那么main线程就会继续执行
             */
            jsc.awaitTermination();


        } catch (Exception e) {
            e.printStackTrace(); // 捕获并打印异常
        } finally {
//            sparkSession.close();
        }
    }

}

